package cn.sxt.bus.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.sxt.bus.domain.Inport;
import cn.sxt.bus.domain.Provider;
import cn.sxt.bus.service.InportService;
import cn.sxt.bus.service.ProviderService;
import cn.sxt.bus.vo.InportVo;
import cn.sxt.bus.vo.ProviderVo;
import cn.sxt.sys.utils.DataGridView;

@Controller
@RequestMapping("inport")
public class InportController {

	@Autowired
	private InportService inportService;
	
	@Autowired
	private ProviderService providerService;

	// 跳到inportManager.jsp
	@RequestMapping("toInportManager")
	public String toInportManager() {
		return "business/inport/inportManager";
	}

	// inportManager.jsp加载 左边
	@RequestMapping("toInportLeft")
	public String toInportLeft() {
		return "business/inport/inportLeft";
	}

	// inportManager.jsp加载 右边
	@RequestMapping("toInportRight")
	public String toInportRight() {
		return "business/inport/inportRight";
	}

	// 进货数据，分页，模糊
	@RequestMapping("queryAllInport")
	@ResponseBody
	public DataGridView queryAllInport(InportVo inportVo) {
		return inportService.queryAllInport(inportVo);
	}

	// 跳添加页面
	@RequestMapping("toAddInport")
	public String toAddInport(Model model) {
		List<Provider> list = providerService.queryAllproviderServiceForList(new ProviderVo());
		model.addAttribute("providers", list);
		return "business/inport/inportAdd";
	}

	// 添加
	@RequestMapping("addInport")
	@ResponseBody
	public Map<String, Object> addInport(InportVo inportVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			inportVo.setInporttime(new Date());
			int i = inportService.addInport(inportVo);
			if (i > 0) {
				msg = "添加成功";
			} else {
				msg = "添加失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "添加失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 跳修改页面，查回显信息
	@RequestMapping("toUpdateInport")
	public String toUpdateInport(InportVo inportVo, Model model) {
		InportVo inport = inportService.queryInportVoById(inportVo.getId());
		model.addAttribute("inport", inport);
		List<Provider> list = providerService.queryAllproviderServiceForList(new ProviderVo());
		model.addAttribute("providers", list);
		return "business/inport/inportUpdate";
	}

	// 修改
	@RequestMapping("updateInport")
	@ResponseBody
	public Map<String, Object> updateInport(InportVo inportVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = inportService.updateInport(inportVo);
			if (i > 0) {
				msg = "修改成功";
			} else {
				msg = "修改失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "修改失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 删除
	@RequestMapping("deleteInport")
	@ResponseBody
	public Map<String, Object> deleteInport(InportVo inportVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = inportService.deleteInportById(inportVo);
			if (i > 0) {
				msg = "删除成功";
			} else {
				msg = "删除失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 批量删除商品
	@RequestMapping("batchDeleteInport")
	@ResponseBody
	public Map<String, Object> batchDeleteInport(InportVo inportVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			Integer[] ids = inportVo.getIds();
			if (ids != null && ids.length > 0) {
				for (int i = 0; i < ids.length; i++) {
					inportVo.setId(ids[i]);
					int j = inportService.deleteInportById(inportVo);
					if (j > 0) {
						msg = "删除成功";
					} else {
						msg = "删除失败";
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}

}
